package com.itheima.web.servlet.store;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Orders;
import com.itheima.utils.BeanUtil;
import com.itheima.web.servlet.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static org.apache.ibatis.ognl.DynamicSubscript.all;

/**
 * @Author:项磊
 * @Description:
 * @Date: Created in 19:03 2020/11/30
 * @ModifiedBy:
 */

@WebServlet("/store/orders")
public class OrdersServlet extends BaseServlet {
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer pageNum = 1;
        Integer pageSize = 5;
        String orderNum = "";

        try {
            pageNum = Integer.valueOf(request.getParameter("pageNum"));
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
            orderNum = request.getParameter("ordersNum");
        } catch (Exception e) {
            e.printStackTrace();
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Orders> ordersList = ordersService.findAll(orderNum);
        PageInfo<Orders> page = new PageInfo<>(ordersList);

        request.setAttribute("page", page);

        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/store/orders/orders-list.jsp").forward(request, response);
    }

//    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //跳转页面
//        request.getRequestDispatcher("/WEB-INF/pages/store/orders/orders-add.jsp").forward(request, response);
//    }

//    public void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//        //将数据获取到，封装成一个对象
//        Orders orders= BeanUtil.fillBean(request, Orders.class,"yyyy-MM-dd");
//        //调用业务层接口save
//        ordersService.save(orders);
//        //跳转回到页面list
//        response.sendRedirect(request.getContextPath()+"/store/orders?operation=orders-list");
//    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = request.getParameter("id");
        Orders orders = ordersService.findOrdersById(id);
        request.setAttribute("orders", orders);
        //跳转页面
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/store/orders/orders-show.jsp").forward(request, response);
    }

//    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        //将数据获取到，封装成一个对象
//        Orders orders= BeanUtil.fillBean(request,Orders.class,"yyyy-MM-dd");
//        //调用业务层接口update
//        ordersService.update(orders);
//        //跳转回到页面list
//        response.sendRedirect(request.getContextPath()+"/store/orders?operation=orders-list");
//    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deleteArrays = "";
        try {
            deleteArrays = request.getParameter("deleteArrays");
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<String> OrdersIds = new ObjectMapper().readValue(deleteArrays, new TypeReference<List<String>>() {
        });
        for (String ordersId : OrdersIds) {
            ordersService.delete(ordersId);
        }
    }
}
